package com.example.demo09redisbloom.service;

import com.example.demo09redisbloom.config.BloomFilterInitializer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author CoderJia
 * @create 2024/10/6 下午 12:37
 * @Description
 **/
@Slf4j
@Service
public class RedisBloomService {

    private static final String BLOOM_FILTER_NAME = BloomFilterInitializer.REDIS_BLOOM__NAME;

    @Autowired
    RedisTemplate<String, Object> redisTemplate;
    @Resource(name = "bloomExistRedisScript")
    private RedisScript<Boolean> bloomExistRedisScript;
    @Resource(name = "bloomAddRedisScript")
    private RedisScript<Boolean> bloomAddRedisScript;

    public Boolean exits(String key) {
        List<String> list = new ArrayList<>(Arrays.asList(BLOOM_FILTER_NAME, key));
        return redisTemplate.execute(bloomExistRedisScript, list);
    }

    public Boolean addOneKey(String key) {
        List<String> list = new ArrayList<>(Arrays.asList(BLOOM_FILTER_NAME, key));
        return redisTemplate.execute(bloomAddRedisScript, list);
    }
}